package cn.kgc.javaweb.dao.impl;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.javaweb.dao.UserDao;
import cn.kgc.javaweb.entity.User;
import cn.kgc.javaweb.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/10/31
 * mysql数据库的处理方案
 */
public class UserDaoImpl  implements UserDao {


    @Override
    public Integer registerUser(User user) {
        //如何将用户数据写入数据库  jdbc  commons-dbutils    结果集处理   user  list
        DataSource dataSource = DruidUtil.getDataSource();

        QueryRunner queryRunner = new QueryRunner(dataSource);

        String sql  = "insert into user (username,password,tel,address,birth)values(?,?,?,?,?)";

        try {

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String birth = simpleDateFormat.format(user.getBirth());

            int line = queryRunner.update(sql, user.getUsername(), user.getPassword(), user.getTel(), user.getAddress(), birth);
            return line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public User findByUserName(String username) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select *  from  user where username = ?";

        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), username);
            return user;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw  new  RuntimeException("数据库操作失败");
        }
    }

    @Override
    public List<User> selectAll() {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select *  from user ";
        try {
            List<User> users = queryRunner.query(sql, new BeanListHandler<User>(User.class));
            return  users;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer updateUserById(User user) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "update user set tel = ? ,address= ? ,birth = ? where id = ?";

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String birth = simpleDateFormat.format(user.getBirth());

        try {
            int line = queryRunner.update(sql, user.getTel(), user.getAddress(), birth,user.getId());
            return line;
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer deleteById(Long id) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "delete from user  where id = ?";
        try {
            int line = queryRunner.update(sql, id);
            return line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer resetPwd(Long id) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "update user set password = ?  where id = ?";
        try {
            int line = queryRunner.update(sql, SecureUtil.md5("888888"), id);
            return  line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> selectPage(Integer start, Integer limit) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select  *  from user limit ?,? ";
        try {
            List<User> users = queryRunner.query(sql, new BeanListHandler<User>(User.class), start, limit);
            return users;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer updateImg(String lastName, Long id) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "update user set img = ?  where id = ?";
        try {
            int line = queryRunner.update(sql, lastName, id);
            return  line;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
